package com.lfk.demo.Ability;

import com.lfk.justwetools.Utils.Toast;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;

import com.lfk.justwetools.Component.FileExplorer.FileExplorer;
import com.lfk.justwetools.Component.FileExplorer.OnFileChosenListener;
import com.lfk.justwetools.Component.FileExplorer.OnPathChangedListener;
import com.lfk.justwetools.Component.Proportionview.ProportionView;
import com.lfk.demo.ResourceTable;
import ohos.agp.components.ListContainer;
import ohos.bundle.IBundleManager;
import ohos.multimodalinput.event.KeyEvent;
import ohos.utils.net.Uri;


public class ExplorerAbility extends Ability {
    private FileExplorer fileExplorer;
    private long exitTime = 0;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setPattern(ResourceTable.Pattern_base);
        setUIContent(ResourceTable.Layout_ability_explorer);
        final ProportionView view = (ProportionView) findComponentById(ResourceTable.Id_pv);

        fileExplorer = (FileExplorer)findComponentById(ResourceTable.Id_ex);
        //覆盖屏蔽原有长按事件
        fileExplorer.setItemLongClickedListener(new ListContainer.ItemLongClickedListener() {
            @Override
            public boolean onItemLongClicked(ListContainer listContainer, Component component, int i, long l) {
                return false;
            }
        });
        //新路径下分析文件比例
        fileExplorer.setOnPathChangedListener(new OnPathChangedListener() {
            @Override
            public void onPathChanged(String path) {
                try {
                    view.setData(fileExplorer.getPropotionText(path));
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "此路径下不可访问或文件夹下无文件", Toast.LENGTH_LONG).show();
                }
            }
        });

        fileExplorer.setOnFileChosenListener(new OnFileChosenListener() {
            @Override
            public void onFileChosen(Uri fileUri) {
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName(getBundleName())
                        .withAbilityName(CodeAbility.class)
                        .withUri(fileUri)
                        .build();
                intent.setOperation(operation);
                startAbility(intent);
            }
        });

        if (verifySelfPermission("ohos.permission.READ_USER_STORAGE") != IBundleManager.PERMISSION_GRANTED) {
            if (canRequestPermission("ohos.permission.READ_USER_STORAGE")) {
                requestPermissionsFromUser(
                        new String[] { "ohos.permission.READ_USER_STORAGE" } , 0);
            }
        } else {
            fileExplorer.setCurrentDir("/sdcard/");
            fileExplorer.setRootDir("/sdcard/");
        }
    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == 0) {
            if (grantResults.length > 0 && grantResults[0] == IBundleManager.PERMISSION_GRANTED) {
                fileExplorer.setCurrentDir("/sdcard/");
                fileExplorer.setRootDir("/sdcard/");
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEY_BACK
                && event.isKeyDown()) {
            if(!fileExplorer.toParentDir()){
                if(System.currentTimeMillis() - exitTime < 1000)
                    terminateAbility();
                exitTime = System.currentTimeMillis();
                Toast.makeText(this, "再次点击退出", Toast.LENGTH_SHORT).show();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
